<?php
class Content
{
	private $content_id;
	private $type;
	private $title;
	private $content;
	private $user_id;
	private $upload_date;
	
	public static function insertContent($type,$content,$user_id,$title)
	{
		DBOperations::insetrtContentIntoDB($type,$content,$user_id,$title);
	}
	
	public static function img_resize( $tmpname, $size, $save_dir, $save_name )
	{

		/**
		 * Make thumbs from JPEG, PNG, GIF source file
		 *
		 * $tmpname = $_FILES['source']['tmp_name'];   
		 * $size - max width size
		 * $save_dir - destination folder
		 * $save_name - tnumb new name
		 *
		 * Author:  LEDok - http://www.citadelavto.ru/
		 */

		$save_dir .= ( substr($save_dir,-1) != "/") ? "/" : "";
		$gis = GetImageSize($tmpname);
		$type = $gis[2];
		switch($type)
			{
			case "1": $imorig = imagecreatefromgif($tmpname); break;
			case "2": $imorig = imagecreatefromjpeg($tmpname);break;
			case "3": $imorig = imagecreatefrompng($tmpname); break;
			default:  $imorig = imagecreatefromjpeg($tmpname);
			}

			$x = imageSX($imorig);
			$y = imageSY($imorig);
			if($gis[0] <= $size)
			{
			$av = $x;
			$ah = $y;
			}
			else
			{
				$yc = $y*1.3333333;
				$d = $x>$yc?$x:$yc;
				$c = $d>$size ? $size/$d : $size;
				  $av = $x*$c;        //высота исходной картинки
				  $ah = $y*$c;        //длина исходной картинки
			}   
			$im = imagecreate($av, $ah);
			$im = imagecreatetruecolor($av,$ah);
		if (imagecopyresampled($im,$imorig , 0,0,0,0,$av,$ah,$x,$y))
			if (imagejpeg($im, $save_dir.$save_name))
				return true;
				else
				return false;
	}
	
	public static function getRemoteFileSize($url) 
	{
		$sch = parse_url($url, PHP_URL_SCHEME);
		if (($sch != "http") && ($sch != "https") && ($sch != "ftp") && ($sch != "ftps")) {
			return false;
		}
		if (($sch == "http") || ($sch == "https")) {
			$headers = get_headers($url, 1);
			if ((!array_key_exists("Content-Length", $headers))) { return false; }
			return $headers["Content-Length"];
		}
		if (($sch == "ftp") || ($sch == "ftps")) 
		{
			$server = parse_url($url, PHP_URL_HOST);
			$port = parse_url($url, PHP_URL_PORT);
			$path = parse_url($url, PHP_URL_PATH);
			$user = parse_url($url, PHP_URL_USER);
			$pass = parse_url($url, PHP_URL_PASS);
			if ((!$server) || (!$path)) { return false; }
			if (!$port) { $port = 21; }
			if (!$user) { $user = "anonymous"; }
			if (!$pass) { $pass = "phpos@"; }
			switch ($sch) {
				case "ftp":
					$ftpid = ftp_connect($server, $port);
					break;
				case "ftps":
					$ftpid = ftp_ssl_connect($server, $port);
					break;
			}
			if (!$ftpid) { return false; }
			$login = ftp_login($ftpid, $user, $pass);
			if (!$login) { return false; }
			$ftpsize = ftp_size($ftpid, $path);
			ftp_close($ftpid);
			if ($ftpsize == -1) { return false; }
			return $ftpsize;
		}
	}
	
	public static function new_name($extension)
	{
		$simbols = "1234567890qwertyuiopasdfgjklzxcvbnm";
		return substr(str_shuffle($simbols), 0, 25).".".$extension;
	}
	
	
	public function getContent_id() 
	{
		return $this->content_id; 
	} 
	public function getType() 
	{
		return $this->type; 
	}
	public function getTitle() 
	{
		return $this->title; 
	}
	public function getContent() 
	{
		return $this->content; 
	} 
	public function getUser_id() 
	{
		return $this->user_id; 
	} 
	public function getUpload_date() 
	{
		return $this->upload_date; 
	} 
	public function setContent_id($x) 
	{
		$this->content_id = $x; 		
	} 
	public function setType($x) 
	{
		$this->type = $x; 		
	} 
	public function setTitle($x) 
	{
		$this->title = $x; 		
	} 
	public function setContent($x) 
	{
		$this->content = $x; 		
	} 
	public function setUser_id($x) 
	{
		$this->user_id = $x; 		
	} 
	public function setUpload_date($x) 
	{
		$this->upload_date = $x; 		
	}
}
?>